package com.frxuexi.myroom.viewmodel

import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.cachedIn
import com.frxuexi.lib_base.BaseViewModel
import com.frxuexi.myroom.intent.LeaveIntent
import com.frxuexi.myroom.paging.MyDataSource
import com.frxuexi.myroom.repository.LeaveRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class LeaveViewModel @Inject constructor(private val leaveRepository: LeaveRepository) :BaseViewModel<LeaveIntent>(){
    override fun handleIntent(it: LeaveIntent) {
        when(it){
            is LeaveIntent.addLeave->
                roomRequest(leaveRepository.addLeave(it.leave))
            is LeaveIntent.getAllEmp->
                roomRequest(leaveRepository.getAllEmp())
            is LeaveIntent.approveLeave->
                roomRequest(leaveRepository.approveLeave(it.leave))
        }
    }

    fun getMyLeaves(uname:String)=Pager(PagingConfig(pageSize = 10)){
        MyDataSource(leaveRepository,uname)
    }.flow.cachedIn(viewModelScope)
}